﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Space_Cleaner.SpaceCleaner
{
    class SC_Display : SC_IMouseListener
    {
        private float zoomLevel;
        private float zoomVel;
        private float width;
        private float height;
        private Vector2 center;
        private Vector2 viewPos;
        private Vector2 mouseVel;
        private Boolean leftPressed = false;

        public SC_Display(float _width, float _height)
        {
            this.width = _width;
            this.height = _height;
            center = new Vector2(this.width / 2.0f, this.height / 2.0f);
            viewPos = new Vector2(0,0);
            zoomLevel = 250;                //initial-Zoom
        }

        public float ZoomLevel
        {
            get { return zoomLevel; }
        }

        public float Scale
        {
            get { return (ZoomLevel / 120.0f + 1) * 0.1f; }
        }

        public void update(TimeSpan elapsedTime)
        {
            zoomVel /= 1.1f;
            zoomLevel += zoomVel * elapsedTime.Milliseconds;

            mouseVel /= 1.1f;
            viewPos += mouseVel * elapsedTime.Milliseconds;


            Utils.limit(ref zoomLevel, 0.5f, 10000.0f);
            Utils.limit(ref viewPos.X, -width / 2.0f / Scale, width / 2.0f / Scale);
            Utils.limit(ref viewPos.Y, -height / 2.0f / Scale, height / 2.0f / Scale);
        }

        public void moveMouse(Vector2 delta)
        {
            if (leftPressed)
               mouseVel += delta* 0.008f / Scale;
        }

        public void zoom(int value)
        {
            zoomVel += value / 500.0f;
        }

        public Vector2 getRelativePosition(Vector2 _realPosition)
        {
            return new Vector2(getRelativeX(_realPosition.X),getRelativeY( _realPosition.Y));
        }

        private float getRelativeX(float _realX)
        {
            return (_realX + viewPos.X) * Scale + center.X;
        }

        private float getRelativeY(float _realY)
        {
            return (_realY + viewPos.Y) * Scale + center.Y;
        }

        public void leftMouseButtonStateChanged(Boolean _pressed)
        {
            leftPressed = _pressed;
        }

        public void onMouseWheelMove(int value)
        {
            this.zoom(value);
        }

        public void onMouseMove(Vector2 delta)
        {
            this.moveMouse(delta);
        }

        public void onLeftButtonPressed()
        {
            this.leftMouseButtonStateChanged(true);
        }

        public void onLeftButtonReleased()
        {
            this.leftMouseButtonStateChanged(false);
        }

        public void onRightButtonPressed()
        {
        }

        public void onRightButtonReleased()
        {
        }
    }
}
